package hu.myai.indicators.tick;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Pivot point is a new trading indicator. It defines levels: Resistence 1..3
 * and Support 1..3. Most case intraday traders use it an set the previous day
 * as source. Pivot point is provided by tickIndicator
 *
 * http://www.youtube.com/watch?v=240BLg3tmuQ&feature=related
 *
 * @author bozbalint
 *
 * */
public class PivotLevelIndicator extends AbstractIndicator {

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public static final int R3 = 3;
    public static final int R2 = 2;
    public static final int R1 = 1;
    public static final int S1 = -1;
    public static final int S2 = -2;
    public static final int S3 = -3;

    public PivotLevelIndicator(Double parameter) {
        super(parameter);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init() {
    }

    @Override
    protected IndicatorVO indicatorCalculation() {

        double pivotPonint = (lastTick.getHigh() + lastTick.getLow() + lastTick.getClose()) / 3;

        IndicatorVO indicator = new IndicatorVO();
        indicator.setChanged(true);
        indicator.setDate(lastTick.getTime());

        if (parameter == S1) {
            double s1 = (pivotPonint * 2) - lastTick.getHigh();
            indicator.setValue(s1);
        } else {

            double s2 = pivotPonint - lastTick.getHigh() + lastTick.getLow();

            if (parameter == S2) {
                indicator.setValue(s2);
            } else

            if (parameter == S3) {
                double s3 = s2 - lastTick.getHigh() + lastTick.getLow();
                indicator.setValue(s3);
            }
        }

        if (parameter == R1) {
            double r1 = (pivotPonint * 2) - lastTick.getLow();
            indicator.setValue(r1);
        } else {

            double r2 = pivotPonint + lastTick.getHigh() - lastTick.getLow();

            if (parameter == R2) {
                indicator.setValue(r2);
            } else

            if (parameter == R3) {
                double r3 = r2 + lastTick.getHigh() - lastTick.getLow();
                indicator.setValue(r3);
            }
        }

        log.debug(indicator.toString());
        return indicator;
    }
}
